package com.course.business.controller.admin;

import com.course.server.dto.*;
import com.course.server.service.CourseCategoryService;
import com.course.server.service.CourseService;
import com.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/course")
public class CourseController {

    private static final Logger LOG = LoggerFactory.getLogger(CourseController.class);
    public static final String BUSINESS_NAME = "课程表";

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseCategoryService courseCategoryService;

    @RequestMapping("/list")
    public ResponseDto<PageDto> test(@RequestBody PageDto pageDto) {
        LOG.info("pageDto:{}", pageDto);
        courseService.list(pageDto);
        return new ResponseDto <>(pageDto);
    }

    @RequestMapping("/save")
    public ResponseDto<CourseDto> save(@RequestBody CourseDto courseDto) {
        LOG.info("courseDto:{}", courseDto);
        // 保存校验
        ValidatorUtil.require(courseDto.getName(), "名称");
        ValidatorUtil.length(courseDto.getName(), "名称", 1 , 50);
        ValidatorUtil.length(courseDto.getSummary(), "概述", 1 , 2000);
        ValidatorUtil.length(courseDto.getImage(), "封面", 1 , 100);

        courseService.save(courseDto);
        return new ResponseDto <>(courseDto);
    }

    @RequestMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        courseService.del(id);
        return new ResponseDto<>("200","删除成功！");
    }

    @PostMapping("/list-catrgory/{courseId}")
    public ResponseDto<List <CourseCategoryDto>> listCatrgory(@PathVariable(value = "courseId") String courseId) {
        ResponseDto<List <CourseCategoryDto>> responseDto = new ResponseDto<>();
        List <CourseCategoryDto> dtoList = courseCategoryService.listByCourse(courseId);
        responseDto.setContent(dtoList);
        return responseDto;
    }

    @GetMapping("/find-content/{id}")
    public ResponseDto<CourseContentDto> findContent(@PathVariable String id) {
        ResponseDto <CourseContentDto> responseDto = new ResponseDto <>();
        CourseContentDto content = courseService.findContent(id);
        responseDto.setContent(content);
        return responseDto;
    }

    @PostMapping("/save-content")
    public ResponseDto <String> saveContent(@RequestBody CourseContentDto contentDto) {
        ResponseDto <String> responseDto = new ResponseDto <>();
        courseService.saveContent(contentDto);
        return responseDto;
    }

    @RequestMapping(value = "/sort")
    public ResponseDto sort(@RequestBody SortDto sortDto) {
        LOG.info("更新排序");
        ResponseDto responseDto = new ResponseDto();
        courseService.sort(sortDto);
        return responseDto;
    }
}